package com.weiyi.weiman.config;

import com.weiyi.weiman.interceptor.AppletInterceptor;
import com.weiyi.weiman.interceptor.IpBlackInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @Author zhang
 * @Date 2021/5/1 下午11:46
 * @Description 拦截器配置类
 * @Version 1.0
 **/
@Configuration
public class InterceptorConfig implements WebMvcConfigurer {
    //在此处，将拦截器注册为一个 Bean
    @Bean
    public AppletInterceptor getAppletInterceptor() {
        return new AppletInterceptor();
    }

    @Bean
    public IpBlackInterceptor getIpBlackInterceptor() {
        return new IpBlackInterceptor();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //需要拦截的路径，/**表示需要拦截所有请求
        String[] addPathPatterns = {"/**"};
        //不需要拦截的路径,当然要包括登录页面
        String[] excludePathPatterns = {};
        //注册登录拦截器
        //order指定执行顺序，数值越小越优先
        //可以配置多个拦截器,给registry再加一个Interceptor就可以了，不用再创建一个新的config配置类。
        //拦截器1如果未登录访问首页跳转登录页
        registry.addInterceptor(getIpBlackInterceptor())
                .addPathPatterns(addPathPatterns)
                .excludePathPatterns(excludePathPatterns)
                .order(0);
        registry.addInterceptor(getAppletInterceptor())
                .addPathPatterns(addPathPatterns)
                .excludePathPatterns(excludePathPatterns)
                .order(0);
    }
}
